from kivy.graphics import Color, Line
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout

from kivy_cust.LabelComm import LabelComm

# 自定义BoxLayout，当做表格的单元格
class CellLayout(BoxLayout):
    padding = [5, 5, 5, 5]

    text = StringProperty('')

    def __init__(self, **kwargs):
        super(CellLayout, self).__init__(**kwargs)

        if kwargs.get('text'):
            self.text = kwargs.get('text')
        if kwargs.get('width'):
            self.width = kwargs.get('width')

    def on_kv_post(self, base_widget):
        # self.size_hint = (None, None)  # 禁用size_hint以使用size属性
        # self.size = self.(100, 45)  # 默认宽度和高度

        label = LabelComm(text=self.text)
        self.add_widget(label)

        with self.canvas.before:
            # 设置边框颜色
            Color(1, 0, 0, 1)

            # 绘制虚线边框
            # 使用 Line 组件，设置 dash 参数来实现虚线效果
            # self.dash_line = Line(rectangle=(self.x, self.y, self.width, self.height),
            #                       dash_length=5,  # 线段长度
            #                       dash_offset=5,  # 空格长度
            #                       width=1)
            # 设置边框
            # self.dash_line = Line(rectangle=(self.x, self.y, self.width, self.height), width=1)
            self.line = Line(rectangle=(self.x, self.y, self.width, self.height), width=1)
            self.bind(size=self.update_rect, pos=self.update_rect)

    def update_rect(self, instance, *args):
        # 更新边框
        # self.dash_line.rectangle = (self.x, self.y, self.width, self.height)
        self.line.rectangle = (self.x, self.y, self.width, self.height)
